package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test4 {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test4.class.getResource(".").toURI());

        String pname = Test4.class.getPackage().getName();

        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File sub : subs) {
            String filename = sub.getName();
            String classname = filename.replaceAll(".class", "");
            filename = pname + "." + classname;
            Class cls = Class.forName(filename);
            if (cls.isAnnotationPresent(AutoRunClass.class)) {
                Object o = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for (Method method :methods) {
                    if(method.isAnnotationPresent(AutoRunMethod.class)){
                        method.invoke(o);
                    }
                }
            }
        }
    }
}
